﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataTransfer;
using EnglishLibrary;
using Global;
using Helper;

namespace EnglishReminder.Functions
{
    public partial class MergeProfile : Form
    {
        #region Properties and constants
        private List<string> _listProfiles;
        private const string DISPLAY_FORMAT = "[{0}] - {1}";
        #endregion
        public MergeProfile()
        {
            InitializeComponent();
        }

        private void MergeProfile_Load(object sender, EventArgs e)
        {
            //Load the current profile
            _listProfiles = new List<string>();
            //_listProfiles.Add(string.Format(DISPLAY_FORMAT,Settings.CURRENT_FILE_NAME, Settings.CURRENT_FILE_PATH));
            //update the listbox
            //UpdateTheDisplayListBox();

            //Setup the open dialog
            openFileDialog1.Filter = Constant.FILE_FILTERS;
        }

        #region Functions
        private void UpdateTheDisplayListBox()
        {
            lbProfiles.Items.Clear();
            foreach (var item in _listProfiles)
                lbProfiles.Items.Add(item);
        }
        private void CleanUp()
        {

        }
        #endregion
        #region Event Handlers
        private void btnCLose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void btnAdd_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = string.Empty;
            var dialogResult = openFileDialog1.ShowDialog();

            switch (dialogResult)
            {
                case DialogResult.Cancel:
                    break;
                case DialogResult.OK:
                    //TODO: Need validate file before open
                    //TODO: Need check duplicate

                    //Check duplicate
                    var newProfile = string.Format(DISPLAY_FORMAT, openFileDialog1.FileName.Split('\\').Last(),
                                                   openFileDialog1.FileName);

                    if (!_listProfiles.Contains(newProfile))
                    {
                        _listProfiles.Add(newProfile);
                        lbProfiles.Items.Add(newProfile);
                    }
                    else
                    {
                        MessageBox.Show("This profile has already been chosen. Please choose another one.");
                    }

                    //Check add current profile
                    if (!_listProfiles.Contains(string.Format(DISPLAY_FORMAT, Settings.CURRENT_FILE_NAME, Settings.CURRENT_FILE_PATH)))
                    {
                        _listProfiles.Add(newProfile);
                        lbProfiles.Items.Add(newProfile);
                    }
                    else
                    {
                        MessageBox.Show("Don't have to add the current profile, all the items will be added into this current profile.");
                    }

                    //UpdateTheDisplayListBox();
                    break;
            }
        }

        private void btnMerge_Click(object sender, EventArgs e)
        {
            //Check if the list has more than 1 profile
            if (_listProfiles.Count <= 1)
            {
                MessageBox.Show("Please add more profile to merge");
                return;
            }

            var crrCateDT = new CategoryDT(Settings.CURRENT_FILE_PATH);
            var crrVocaDT = new VocabularyDT(Settings.CURRENT_FILE_PATH);

            //key : cateID
            //value : list of vocabulary
            var vocProLists = new Dictionary<string, List<Vocabulary>>();
            foreach (var profile in _listProfiles)
            {
                var cateDT = new CategoryDT(profile);
                var cateList = cateDT.GetListCategories();
                
                foreach (var cate in cateList)
                {
                    //crrCateDT
                }
                //vocabularyDT.FilePath = profile;
                //vocProLists.Add(vocabularyDT.GetListVocabulary(true));
            }

            //if (ProfileHelper.Merge(vocProLists))
            //{
            //    MessageBox.Show("Success");
            //}
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            _listProfiles.Remove(lbProfiles.SelectedItem.ToString());
            lbProfiles.Items.Remove(lbProfiles.SelectedItem);
        }
    }
}
